<?php
namespace App\Factory\Payment;

use App\Utils\Other\Sandpay\Payment\Mini;
use App\Factory\Wechat\Factory as WechatFactory;
use EasyWeChat\Kernel\HttpClient\HttpClientMethods;
use App\Utils\Other\Safepay\Payment\Mini as SafepayMini;
use function Hyperf\Config\config;
use function Hyperf\Support\make;

Class Factory {

    /**
     * 衫德微信小程序支付
     * @param array $params
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function sandpayMini(array $params):array
    {
        /** @var Mini $miniPay */
        $miniPay = make(Mini::class);

        $params['totalAmount'] = str_pad($params['amount'] * 100, 12, '0', STR_PAD_LEFT);
        $params['orderCode'] = $params['out_trade_no'];
        $params['orderBody'] = $params['description'];

        unset($params['amount'], $params['out_trade_no'], $params['description']);

        return $miniPay->handle($params);
    }

    /**
     * 微信小程序支付
     * @param array $params
     * @return array
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
     */
    public function wechatMini(array $params):array
    {
        /** @var WechatFactory $pay */
        $pay = make(WechatFactory::class);
        $app = $pay->pay();
        $account = $app->getMerchant();

        $params['appid'] = config('cloud.mini.app_id');
        $params['mchid'] = $account->getMerchantId();
        $params['payer']['openid'] = $params['openid'];
        $params['amount'] = [
            'total' => $params['amount'] * 100,
            'currency' => 'CNY'
        ];
        $params['notify_url'] = getDomain()."/school/v1/payment/notify/wechatMini";
        unset($params['amount'], $params['openid']);

        /** @var HttpClientMethods $client */
        $client = $app->getClient();

        $response = $client->postJson('v3/pay/transactions/jsapi', $params);
        $data = $response->toArray();
        $app->getValidator()->validate($response->toPsrResponse());

        $prepayId = $data['prepay_id'];

        $utils = $app->getUtils();
        $config = $utils->buildMiniAppConfig($prepayId, $params['appid']);

        return [$params, $response->toArray(), $config];
    }

    /**
     * safepay小程序支付
     * @param array $params
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function safepayMini(array $params, string $appId = ''):array
    {
        /** @var SafepayMini $miniPay */
        $miniPay = make(SafepayMini::class);

        $params['tradeamt'] = $params['amount'];
        $params['orderCode'] = $params['out_trade_no'];
        $params['body'] = $params['description'];
        $params['payId'] = '11';

        unset($params['amount'], $params['out_trade_no'], $params['description']);

        return $miniPay->handle($params, $appId);
    }
}